package mybox.engine;


public class Rectangle implements Figur {
	
	public File4Qcad file4qcad=null;
	QVector lowerLeft=null;
	QVector upperRight=null;
	QVector lowerRight=null;
	QVector upperLeft=null;
	
	QVector lowerLeft2lowerRight=null;
	QVector lowerLeft2upperLeft=null;	
	
	
	QVector[] jamaicaMiddle= new QVector[4];
	
	double laenge;
	double breite;	
	double alpha;
	
	public Rectangle(File4Qcad file4qcad, QVector lowerLeft, QVector upperRight) {
		this.file4qcad=file4qcad;
		this.lowerLeft=lowerLeft;
		this.upperRight=upperRight;
		init();		
	}
	
	public Rectangle(File4Qcad file4qcad, QVector lowerLeft, double laenge, double breite) {
		this.file4qcad=file4qcad;
		this.lowerLeft=lowerLeft;
		this.upperRight=new QVector(lowerLeft.x+laenge,lowerLeft.y+breite);
		init();		
	}
	
	public Rectangle(File4Qcad file4qcad, QVector lowerLeft, double laenge, double breite, double phi) {
		this.file4qcad=file4qcad;
		this.lowerLeft=lowerLeft;
		lowerLeft2lowerRight=new QVector(laenge*java.lang.Math.cos(phi),laenge*java.lang.Math.sin(phi));
		lowerLeft2upperLeft=new QVector(-breite*java.lang.Math.sin(phi),breite*java.lang.Math.cos(phi));		
		init();		
	}
	
	public void init() {
		
		if (lowerLeft2lowerRight!=null && lowerLeft2upperLeft!=null) {
			
			lowerRight=new QVector(lowerLeft.x+lowerLeft2lowerRight.x,lowerLeft.y+lowerLeft2lowerRight.y);
			upperLeft=new QVector(lowerLeft.x+lowerLeft2upperLeft.x,lowerLeft.y+lowerLeft2upperLeft.y);
			upperRight=new QVector(lowerLeft.x+lowerLeft2lowerRight.x+lowerLeft2upperLeft.x,
								   lowerLeft.y+lowerLeft2lowerRight.y+lowerLeft2upperLeft.y);
			alpha = java.lang.Math.atan(breite/laenge);
						
			
		} else {
			lowerLeft2lowerRight = new QVector(upperRight.x-lowerLeft.x,0);
			lowerLeft2upperLeft = new QVector(0,upperRight.y-lowerLeft.y);
			lowerRight = new QVector(upperRight.x,lowerLeft.y);
			upperLeft = new QVector(lowerLeft.x,upperRight.y);
			laenge = upperRight.x-lowerLeft.x;
			breite = upperRight.y-lowerLeft.y;
			alpha = java.lang.Math.atan(breite/laenge);
		}
	}
	
	public void draw() {		
		file4qcad.println(lowerLeft2lowerRight.printLineAktPosNeutral(lowerLeft));
		file4qcad.println(lowerLeft2upperLeft.printLineAktPosNeutral(lowerRight));
		file4qcad.println(lowerLeft2upperLeft.printLineAktPosNeutral(lowerLeft));
		file4qcad.println(lowerLeft2lowerRight.printLineAktPosNeutral(upperLeft));		
	}
	
	public void draw(boolean unten, boolean rechts, boolean oben, boolean links) {
		if (unten) 	file4qcad.println(lowerLeft2lowerRight.printLineAktPosNeutral(lowerLeft));
		if (rechts) file4qcad.println(lowerLeft2upperLeft.printLineAktPosNeutral(lowerRight));
		if (links) 	file4qcad.println(lowerLeft2upperLeft.printLineAktPosNeutral(lowerLeft));
		if (oben) 	file4qcad.println(lowerLeft2lowerRight.printLineAktPosNeutral(upperLeft));
	}
	
	public void drawJamaica(double jamaicaBreite) {
		
		double sLaenge = jamaicaBreite/(java.lang.Math.sin(alpha)*2);
		
		jamaicaMiddle[0] = new QVector(lowerLeft.x+laenge/2,lowerLeft.y+java.lang.Math.tan(alpha)*(laenge/2-sLaenge));		
		new Triangle(
				file4qcad,
				new QVector(lowerLeft.x+sLaenge,lowerLeft.y),
				new QVector(lowerRight.x-sLaenge,lowerRight.y),
				jamaicaMiddle[0]
				);
		
		
		double PIHalbe = java.lang.Math.PI/2;
		double sBreite = jamaicaBreite/(java.lang.Math.cos(alpha)*2);
		
		
		jamaicaMiddle[1] = new QVector(lowerLeft.x+java.lang.Math.tan(PIHalbe-alpha)*(breite/2-sBreite),lowerLeft.y+breite/2);		
		new Triangle(
				file4qcad,
				new QVector(lowerLeft.x,lowerLeft.y+sBreite),
				new QVector(upperLeft.x,upperLeft.y-sBreite),
				jamaicaMiddle[1]
				);
		
		
		jamaicaMiddle[2] = new QVector(upperLeft.x+laenge/2,upperLeft.y-java.lang.Math.tan(alpha)*(laenge/2-sLaenge));		
		new Triangle(
				file4qcad,
				new QVector(upperLeft.x+sLaenge,upperLeft.y),
				new QVector(upperRight.x-sLaenge,upperRight.y),
				jamaicaMiddle[2]
				);
		
		
		jamaicaMiddle[3] = new QVector(lowerRight.x-java.lang.Math.tan(PIHalbe-alpha)*(breite/2-sBreite),lowerRight.y+breite/2);		
		new Triangle(
				file4qcad,
				new QVector(lowerRight.x,lowerRight.y+sBreite),
				new QVector(upperRight.x,upperRight.y-sBreite),
				jamaicaMiddle[3]
				);
		
		
	}

}
